package com.zhuangwei.sell.controller;

import com.zhuangwei.sell.converter.OrderForm2OrderDtoConverter;
import com.zhuangwei.sell.dto.OrderDto;
import com.zhuangwei.sell.enums.ResultEnum;
import com.zhuangwei.sell.exception.SellException;
import com.zhuangwei.sell.form.OrderForm;
import com.zhuangwei.sell.service.BuyerService;
import com.zhuangwei.sell.service.OrderService;
import com.zhuangwei.sell.utils.ResultVoUtil;
import com.zhuangwei.sell.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author LeviCoder
 * @create 2019-03-02 23:45
 */
@RestController
@RequestMapping("/buyer/order")
@Slf4j
public class BuyerOrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private BuyerService buyerService;

    //创建订单
    @PostMapping("/creat")
    public ResultVo<Map<String, String>> createOrder(@Validated OrderForm orderForm,
                                                     BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            log.error("【创建订单】参数不正确，orderForm={}", orderForm);
            throw new SellException(ResultEnum.PARAM_ERROR.getCode(),
                    bindingResult.getFieldError().getDefaultMessage());
        }

        OrderDto orderDto = orderService.create(OrderForm2OrderDtoConverter.convert(orderForm));

        if (CollectionUtils.isEmpty(orderDto.getOrderDetailList())) {
            log.error("【创建订单】购物车不能为空");
            throw new SellException(ResultEnum.CART_NOT_EMPTY);
        }

        OrderDto orderDto1 = orderService.create(orderDto);

        HashMap<String, String> map = new HashMap<>();
        map.put("orderId", orderDto1.getOrderId());

        return ResultVoUtil.success(map);
    }

    //订单列表
    @GetMapping("/list")
    public ResultVo<List<OrderDto>> list(@RequestParam("openid") String openid
            , @RequestParam(value = "page", defaultValue = "0") Integer page
            , @RequestParam(value = "size", defaultValue = "10") Integer size) {
        if (StringUtils.isEmpty(openid)) {
            log.error("【查询订单列表】openid不能为空");
            throw new SellException(ResultEnum.PARAM_ERROR);
        }

        PageRequest pageRequest = new PageRequest(page, size);
        Page<OrderDto> orderDtoPage = orderService.findList(openid, pageRequest);

        return ResultVoUtil.success(orderDtoPage.getContent());
    }

    //订单详情
    @GetMapping("/detail")
    public ResultVo<OrderDto> detail(@RequestParam("openid") String openId, @RequestParam("orderId") String orderId) {

        OrderDto orderDto = buyerService.findOrderOne(openId, orderId);

        return ResultVoUtil.success(orderDto);
    }

    //取消订单
    @PostMapping("/cancel")
    public ResultVo cancel(@RequestParam("openid") String openId,
                           @RequestParam("orderId") String orderId) {
        buyerService.cancel(openId, orderId);
        return ResultVoUtil.success();

    }


}
